package Born.System
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.system.System;
	import flash.text.TextField;
	import flash.text.TextFormat;

	public class Memory extends MovieClip
	{		
		private var Monitor					:MovieClip;
		private var MonitorQuad				:Sprite;
		private var Text					:TextField;
		private var Textformat				:TextFormat;
		private var MemoryFull				:String;
		
		public function Memory()
		{
			
		}
		/**
		 * Create and show memory monitor<br /> 
		 * inside your flash application
		 */
		public function show():void
		{
			Textformat = new TextFormat();
			Textformat.align = "center";
			Textformat.color = 0xffffff;
			Textformat.bold = true;
			Textformat.size = 10;
			Textformat.font = "Verdana";
			
			Text = new TextField();
			Text.width = 75;
			Text.selectable = false;
			Text.multiline = false;
			Text.defaultTextFormat = Textformat;
			Text.addEventListener(MouseEvent.MOUSE_OVER, showCloseMess);
			Text.addEventListener(MouseEvent.MOUSE_OUT, hideCloseMess);
			
			MonitorQuad = new Sprite();
			MonitorQuad.graphics.beginFill(0x212121,0.7);
			MonitorQuad.graphics.drawRect(0,0,75,25);
			MonitorQuad.graphics.endFill();
			
			Monitor = new MovieClip();
			Monitor.addChild(MonitorQuad);
			Monitor.addChild(Text);
			this.addChild(Monitor);
			
			this.addEventListener(MouseEvent.CLICK, hideFullMemory);
			this.addEventListener(Event.ENTER_FRAME, showFullMemory);
		} // End show()
		/**
		 * Show in the textfield close question 
		 * @param e
		 */		
		private function showCloseMess(e:MouseEvent):void
		{
			this.removeEventListener(Event.ENTER_FRAME, showFullMemory);
			Text.text = "Close?";
		}
		/**
		 * Hide it 
		 * @param e
		 */		
		private function hideCloseMess(e:MouseEvent):void
		{
			this.addEventListener(Event.ENTER_FRAME, showFullMemory);
		}
		/**
		 * Calcule and setup data to show in Textfield 
		 * @param e
		 */		
		private function showFullMemory(e:Event):void
		{
			MemoryFull = String(Number( System.totalMemory / 1024 / 1024 ).toFixed(2) + " Mb");
			Text.text = MemoryFull;
		}
		/**
		 * Will call the function to close and <br />
		 * moreve all the listeners 
		 * @param e
		 */		
		private function hideFullMemory(e:MouseEvent):void
		{
			hide();
		}
		/**
		 * Will call Garbage Collector to burn<br /> 
		 * Flash memory trash
		 */		
		public function garbageCollector():void
		{
			for ( var mi:uint = 0; mi < 20; mi++ )
			{
				System.gc();
			}
		}
		/**
		 * Will delete Monitor and<br />
		 * remove all the listeners 
		 * 
		 */		
		public function hide():void
		{
			Monitor.removeChild(MonitorQuad);
			Monitor.removeChild(Text);
			this.removeChild(Monitor);
			this.removeEventListener(Event.ENTER_FRAME, showFullMemory);
		}
		
	} // End Class
} // End Package